package com.marvels.financial.collection.api.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.marvels.common.constants.PageConstant;
import com.marvels.common.dto.MarvelsResultDTO;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.exception.MarvelsErrorCode;
import com.marvels.common.utils.MarvelsLogUtil;
import com.marvels.financial.collection.api.LoanAccountApi;
import com.marvels.financial.collection.dao.McfeOverdueManageMapper;
import com.marvels.financial.collection.dto.CollectAccountDTO;
import com.marvels.financial.collection.dto.CommonConditionDTO;
import com.marvels.financial.loan.dao.McfeCustomerRepaymentPlanDao;
import com.marvels.financial.loan.dao.McfeLendingProcessDao;
import com.marvels.financial.loan.dto.McfeCustomerRepaymentPlanDTO;
import com.marvels.financial.loan.dto.McfeLendingProcess;
import com.marvels.financial.loan.dto.QueryPayPlanInfoDTO;

@Service
public class LoanAccountApiImpl implements LoanAccountApi {
	@Autowired
	private McfeOverdueManageMapper mcfeOverdueManageMapper; 
	@Autowired
	private McfeCustomerRepaymentPlanDao planDao;
	@Autowired
	private McfeLendingProcessDao mcfeLendingProcessDao;
	@Override
	public CollectAccountDTO selectAccountInfo(String loanId) throws MarvelsBusinessException {
		return mcfeOverdueManageMapper.selectLoanContractInfo(loanId);
	}
	@Override
	public PageInfo<McfeCustomerRepaymentPlanDTO> planList(CommonConditionDTO dto) throws MarvelsBusinessException {
		int pageSize=dto.getPageSize();
		int pageNum=dto.getCurrentPage();
		if ( pageSize< 1) {
			pageSize=PageConstant.PAGESIZE;
		}
		if (pageNum< 1) {
			pageNum=PageConstant.CURRPAGE;
		}
		PageHelper.startPage(pageNum,pageSize);
		List<McfeCustomerRepaymentPlanDTO> list = planDao.selectAllPlan(dto.getLoanId());
		PageInfo<McfeCustomerRepaymentPlanDTO> pageInfo = new PageInfo<McfeCustomerRepaymentPlanDTO>(list);
		return pageInfo;
	}
	@Override
	public MarvelsResultDTO queryPayPlanInfo(CommonConditionDTO dto) throws MarvelsBusinessException{
		MarvelsLogUtil.getInstance().info("----------------------获取贷款计划dto" + JSON.toJSONString(dto));
		String loanId = dto.getLoanId();
		
		List<QueryPayPlanInfoDTO> payPlanList = planDao.selectQueryPayPlanInfoList(loanId);
		
		McfeLendingProcess mcfeLendingProcess = mcfeLendingProcessDao.selectByloanIdAndStatus(loanId, null);
		// 贷款记录不存在
		if (mcfeLendingProcess == null) {
			MarvelsLogUtil.getInstance().error("----------------------贷款计划生成失败 loanInfo is null");
			return MarvelsResultDTO.getFailResult(MarvelsErrorCode.FAIL.getValue(), MarvelsErrorCode.FAIL.getDesc());
		}
		// 还款账号
		String repayAcct = mcfeLendingProcess.getPaymentAcctNo();
		// 贷款总期数
		Integer applyTerms = mcfeLendingProcess.getTerms();
		// 还款银行
		String repayBankName = mcfeLendingProcess.getPaymentBankName();

		List<Map<String, Object>> payPlans = null;
		if (payPlanList != null && payPlanList.size() > 0) {
			// 还款计划
			payPlans = new ArrayList<>();
			// 日期 金没
			for (QueryPayPlanInfoDTO queryPayPlanInfoDTO : payPlanList) {
				Map<String, Object> payPlan = new HashMap<>();
				payPlan.put("tenor", queryPayPlanInfoDTO.getTenor());
				payPlan.put("amount", queryPayPlanInfoDTO.getAmount().setScale(2));
				payPlan.put("paymentDate", queryPayPlanInfoDTO.getPaymentDateLate().getTime());
				payPlans.add(payPlan);
			}
		} else {
			MarvelsLogUtil.getInstance().error("----------------------贷款计划生成失败 payPlanList is null");
			return MarvelsResultDTO.getFailResult(MarvelsErrorCode.FAIL.getValue(), MarvelsErrorCode.FAIL.getDesc());
		}

		// 封装返回参数
		Map<String, Object> responseMap = new HashMap<>();
		responseMap.put("repayAcct", repayAcct);
		responseMap.put("applyTerms", applyTerms);
		responseMap.put("repayBankName", repayBankName);
		responseMap.put("payPlans", payPlans);
		return MarvelsResultDTO.getSuccessResult(responseMap);
	}
	@Override
	public Map<String, String> selectAccountOverdueInfo(List<String> loanIds) throws MarvelsBusinessException {
		if(loanIds == null || loanIds.size() == 0){
			return null;
		}
		Map<String, String> result = new HashMap<String, String>();
		List<Map<String, String>> list = mcfeOverdueManageMapper.selectAccountOverdueInfo(loanIds);
		if(list != null){
			for (Map<String, String> map : list) {
				result.put(map.get("loanId"), map.get("status"));
			}
		}
		return result;
	}

}
